package com.hsy.linemoney.modules.common;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.deparse.dpframe.tools.utils.DPToastUtil;
import com.deparse.dpframe.ui.android.app.activity.DPBaseActivity;
import com.hsy.linemoney.R;
import com.hsy.linemoney.common.Config;
import com.hsy.linemoney.apiservice.LineMoneyContext;
import com.hsy.linemoney.modules.lanuch.SplashActivity;
import com.hsy.linemoney.utils.CommonUtils;

/**
 * Created by MartinKent on 2016/9/7.
 */
public class LocalSetActivity extends CommonBaseActivity implements View.OnClickListener {
    protected TextView selectCountrySpinner;
    protected TextView selectLangSpinner;
    protected TextView confirm;

    private int countryPos = -1, langPos = -1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.local_set_dialog_layout);
        initView();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.confirm) {
            if (countryPos < 0 || langPos < 0) {
                DPToastUtil.showToast(this, R.string.txt_info_not_complete);
                return;
            }
            if (countryPos >= 0) {
                LineMoneyContext.setLocalCountryPair(Config.availableCountry.get(langPos));
            }
            if (langPos >= 0) {
                LineMoneyContext.setLocalLanguagePair(Config.availableLang.get(langPos));
            }
            setResult(RESULT_OK);
            startActivityWithTaskCleared(SplashActivity.class);
            overridePendingTransition(R.anim.dp_slide_in_left, R.anim.dp_slide_out_right);
            finish();
        } else if (view.getId() == R.id.select_country_spinner) {
            final String[] items = new String[Config.availableCountry.size()];
            for (int i = 0; i < Config.availableCountry.size(); i++) {
                items[i] = Config.availableCountry.get(i).name;
            }
            CommonUtils.showListDialog(this, items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    selectCountrySpinner.setText(items[which]);
                    countryPos = which;
                }
            });
        } else if (view.getId() == R.id.select_lang_spinner) {
            final String[] items = new String[Config.availableLang.size()];
            for (int i = 0; i < Config.availableLang.size(); i++) {
                items[i] = Config.availableLang.get(i).name;
            }
            CommonUtils.showListDialog(this, items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    selectLangSpinner.setText(items[which]);
                    langPos = which;
                }
            });
        }
    }

    private void initView() {
        selectCountrySpinner = (TextView) findViewById(R.id.select_country_spinner);
        selectCountrySpinner.setOnClickListener(this);
        selectLangSpinner = (TextView) findViewById(R.id.select_lang_spinner);
        selectLangSpinner.setOnClickListener(this);
        confirm = (TextView) findViewById(R.id.confirm);
        confirm.setOnClickListener(this);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!LineMoneyContext.isLocalSetted()) {
                moveTaskToBack(true);
                return true;
            }
        }
        return super.onKeyUp(keyCode, event);
    }
}
